/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * updateDataMobil.java
 *
 * Created on Jan 15, 2012, 4:07:33 AM
 */
package rentalmobil.gui;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import rentalmobil.entity.dataMobil;
import rentalmobil.koneksidatabase.koneksiDataBase;

/**
 *
 * @author Henfryandie
 */
public class updateDataMobil extends java.awt.Dialog {

    dataMobil tambah = new dataMobil();
    List<dataMobil> listDataMobil = new ArrayList<dataMobil>();

    /** Creates new form updateDataMobil */
    public updateDataMobil(java.awt.Frame parent, boolean modal) throws SQLException {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        this.tampilDataMobil();
        tableDataMobil.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                int row = tableDataMobil.getSelectedRow();
                if (row != -1) {
                    isiteks();
                }
            }
        });
    }

    public void kosongkanTextField() {
        txtNoPlatMobil.setText("");
        comboKondisiMobil.setSelectedIndex(0);
        comboStatusMobil.setSelectedIndex(0);
        txtTarifSewa.setText("");
    }
    //method untk mnmpilkan dta mbil dr dlam dtabase k dlm tbel

    public void tampilDataMobil() throws SQLException {
        listDataMobil = tambah.viewDataMobil();
        Object data[][] = new Object[listDataMobil.size()][4];
        int x = 0;
        for (dataMobil d : listDataMobil) {
            data[x][0] = d.getNo_plat_mobil();
            data[x][1] = d.getKondisi_Mobil();
            data[x][2] = d.getStatus_mobil();
            data[x][3] = d.getTarif_sewa();
            x++;
        }
        String judul[] = {"No Plat Mobil", "Kondisi Mobil", "Status Mobil", "Tarif Sewa"};
        tableDataMobil.setModel(new DefaultTableModel(data, judul));
        jScrollPane1.setViewportView(tableDataMobil);
    }

    public void isiteks() {
        dataMobil d = listDataMobil.get(tableDataMobil.getSelectedRow());
        txtNoPlatMobil.setText(d.getNo_plat_mobil());
        if (d.getKondisi_Mobil().equals("Bagus")) {
            comboKondisiMobil.setSelectedIndex(0);
        } else {
            comboKondisiMobil.setSelectedIndex(1);
        }
        if (d.getStatus_mobil().equals("Ada")) {
            comboStatusMobil.setSelectedIndex(0);
        } else if (d.getStatus_mobil().equals("Disewa")) {
            comboStatusMobil.setSelectedIndex(1);
        } else {
            comboStatusMobil.setSelectedIndex(2);
        }
        txtTarifSewa.setText(String.valueOf(d.getTarif_sewa()));
        ButtonTambah.setText("Ubah");
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        ButtonTambah = new javax.swing.JButton();
        ButtonReset = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableDataMobil = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        labelPlat = new javax.swing.JLabel();
        txtNoPlatMobil = new javax.swing.JTextField();
        labelKondisi = new javax.swing.JLabel();
        labelStatus = new javax.swing.JLabel();
        labelTarifSewa = new javax.swing.JLabel();
        txtTarifSewa = new javax.swing.JTextField();
        comboKondisiMobil = new javax.swing.JComboBox();
        comboStatusMobil = new javax.swing.JComboBox();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 255, 204));

        jPanel2.setBackground(new java.awt.Color(153, 255, 153));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 24));
        jLabel1.setText("Update DataMobil");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(312, 312, 312)
                .addComponent(jLabel1)
                .addContainerGap(317, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jPanel3.setBackground(new java.awt.Color(153, 255, 153));
        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        ButtonTambah.setText("Tambah");
        ButtonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonTambahActionPerformed(evt);
            }
        });

        ButtonReset.setText("Reset");
        ButtonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonResetActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Print");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(ButtonTambah)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ButtonReset)
                .addGap(5, 5, 5)
                .addComponent(cancelButton)
                .addGap(5, 5, 5)
                .addComponent(jButton1)
                .addContainerGap(47, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {ButtonReset, ButtonTambah, cancelButton, jButton1});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(ButtonReset)
                        .addComponent(ButtonTambah))
                    .addComponent(cancelButton)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tableDataMobil.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "no_plat_mobil", "Kondisi_mobil", "status_mobil", "tarif_sewa_mobil"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tableDataMobil);

        jPanel4.setBackground(new java.awt.Color(153, 255, 153));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Data Mobil"));

        labelPlat.setText("No Plat Mobil");

        labelKondisi.setText("Kondisi Mobil");

        labelStatus.setText("Status Mobil");

        labelTarifSewa.setText("Tarif Sewa");

        comboKondisiMobil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bagus", "Rusak" }));

        comboStatusMobil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ada", "Disewa", "Di Bengkel" }));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelPlat)
                    .addComponent(labelKondisi)
                    .addComponent(labelStatus)
                    .addComponent(labelTarifSewa))
                .addGap(26, 26, 26)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comboStatusMobil, 0, 265, Short.MAX_VALUE)
                    .addComponent(txtTarifSewa, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboKondisiMobil, javax.swing.GroupLayout.Alignment.TRAILING, 0, 265, Short.MAX_VALUE)
                    .addComponent(txtNoPlatMobil, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPlat)
                    .addComponent(txtNoPlatMobil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelKondisi)
                    .addComponent(comboKondisiMobil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelStatus)
                    .addComponent(comboStatusMobil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTarifSewa)
                    .addComponent(txtTarifSewa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void ButtonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonTambahActionPerformed
        try {
            if (txtNoPlatMobil.getText().isEmpty() || txtTarifSewa.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "Mohon isi data secara lengkap","Warning", JOptionPane.WARNING_MESSAGE);
            } else {
                if (ButtonTambah.getText().equals("Tambah")) {
                    tambah.setNo_plat_mobil(txtNoPlatMobil.getText());
                    tambah.setKondisi_Mobil(comboKondisiMobil.getSelectedItem().toString());
                    tambah.setStatus_mobil(comboStatusMobil.getSelectedItem().toString());
                    tambah.setTarif_sewa(Integer.valueOf(txtTarifSewa.getText()));
                    tambah.insertDataMobil();
                    JOptionPane.showMessageDialog(this, "Data Sudah Tersimpan");
                    this.kosongkanTextField();
                    this.tampilDataMobil();
                } else if (ButtonTambah.getText().equals("Ubah")) {
                    tambah.setNo_plat_mobil(txtNoPlatMobil.getText());
                    tambah.setKondisi_Mobil(comboKondisiMobil.getSelectedItem().toString());
                    tambah.setStatus_mobil(comboStatusMobil.getSelectedItem().toString());
                    tambah.setTarif_sewa(Integer.valueOf(txtTarifSewa.getText()));
                    tambah.updateDataMobil();
                    ButtonTambah.setText("Tambah");
                    JOptionPane.showMessageDialog(this, "Data Sudah di Update");
                    this.kosongkanTextField();
                    this.tampilDataMobil();
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(updateDataMobil.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_ButtonTambahActionPerformed

    private void ButtonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonResetActionPerformed
        // TODO add your handling code here:
        this.kosongkanTextField();
        ButtonTambah.setText("Tambah");
}//GEN-LAST:event_ButtonResetActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String sumberlap = "";
        HashMap hm = new HashMap();
        hm.put("LapDataMobil", "dokumenrental");
        sumberlap = "src\\rentalmobil\\laporan\\LapDataMobil.jrxml";
        try {
            try {
                JasperReport japserRepoert = null;
                try {
                    japserRepoert = JasperCompileManager.compileReport(sumberlap);
                } catch (JRException ex) {
                    Logger.getLogger(updateDataMobil.class.getName()).log(Level.SEVERE, null, ex);
                }
                JasperPrint jprint = JasperFillManager.fillReport(japserRepoert, hm, koneksiDataBase.getConnection());
                JasperViewer.viewReport(jprint);
            } catch (JRException ex) {
                Logger.getLogger(updateDataMobil.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(updateDataMobil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                updateDataMobil dialog = null;
                try {
                    dialog = new updateDataMobil(new java.awt.Frame(), true);
                } catch (SQLException ex) {
                    Logger.getLogger(updateDataMobil.class.getName()).log(Level.SEVERE, null, ex);
                }
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonReset;
    private javax.swing.JButton ButtonTambah;
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox comboKondisiMobil;
    private javax.swing.JComboBox comboStatusMobil;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelKondisi;
    private javax.swing.JLabel labelPlat;
    private javax.swing.JLabel labelStatus;
    private javax.swing.JLabel labelTarifSewa;
    private javax.swing.JTable tableDataMobil;
    private javax.swing.JTextField txtNoPlatMobil;
    private javax.swing.JTextField txtTarifSewa;
    // End of variables declaration//GEN-END:variables
}
